#include "mapelement.h"
#include <iostream>
#include <cmath>


#include <QPainter>
#include <QStyleOption>



void MapElement::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{

     painter->setPen(QPen(Qt::black,4));
     painter->drawLine(m_p1,m_p2);

}

bool MapElement::isColliding(QPointF p)
{
    int x=p.x();
    int y=p.y();

    int A=m_p1.y()-m_p2.y();
    int B=m_p2.x()-m_p1.x();
    int C=m_p2.y()*m_p1.x()-m_p1.y()*m_p2.x();

    float d=std::abs(A*x+B*y+C)/(std::sqrt(A*A+B*B));


        if(y<std::min(m_p1.y(),m_p2.y())-10||x<std::min(m_p1.x(),m_p2.x())-10||y>std::max(m_p1.y(),m_p2.y())+10||x>std::max(m_p1.x(),m_p2.x())+10)
        {
             return false;
        }else if(d<20){
         //     std::cout<<d<<std::endl;
            return true;
        }


    return false;

}

MapElement::MapElement(QPoint p1,QPoint p2)
{
    m_p1=p1;
    m_p2=p2;

    m_boundingRect=QRectF(-10,-10,20,20);
}

QRectF MapElement::boundingRect() const
{
    return m_boundingRect;
}

void MapElement::setBoundingRect(QRectF rect)
{
    m_boundingRect=rect;
}
